Woordenschat = function(){
	
	this.user = '';
	
	this.load = function(){
		forms.load();
		$('#navigation a').click(function(){
			var link = this.href;
			link = link.substr(link.lastIndexOf('#')+1);
			ws.send(link);
		});
		$('.error-message').click(function(){
			$('.error-message').text('');
		});
	}
	
	this.page = function(){
		$('#content').empty();
		return $('#content');
	}
	
	this.getWords = function(){
		ws.send('getWords', {});
	}
	
	this.send = function(action, params){
		// if(callback[action] == undefined)
			// return;
		if(params == undefined)
			params = {};
		params['task'] = action;
		$.ajax({
			url: 'php/',
			data: params,
			dataType: 'json',
			type: 'post',
			beforeSend: function(){
				if(callback[action] == undefined) return;
				//$('#container .load-message').show(200);
				$('#container .error-message').text('');
				if(callback[action].before != undefined) callback[action].before(params);
			},
			success: function(data){
				if(callback[action] == undefined) return;
				if(data.success){
					if(callback[action].done != undefined) callback[action].done(data.result, params);
				}else{
					$('#container .error-message').text(data.result);
					if(callback[action].fail != undefined) callback[action].fail(data.result, params);
				}
			},
			error: function(jqXHR, textStatus, errorThrown){
				if(callback[action] == undefined) return;
				$('#container .error-message').text(textStatus);
				if(callback[action].fail != undefined) callback[action].fail(textStatus, params);
			},
			complete: function(){
				if(callback[action] == undefined) return;
				//$('#container .load-message').hide(200);
				if(callback[action].complete != undefined) callback[action].complete(params);
			}
		});
	}
}

ws = new Woordenschat();
ws.load();

var callback = {
	
	login:{
		done: function(data, params){
			$page = ws.page();
			ws.user = data;
			$('#login-panel').hide();
			$('#user-panel').show();
			$('#username').text(data.username);
			ws.send('getWords');
		}
	},
	
	logout:{
		done: function(data, params){
			$page = ws.page();
			$page.text('Welkom bij Woordenschat');
			$('#user-panel').hide();
			$('#login-panel').show();
		}
	},
	
	getWords:{
		done: function(data, params){
			$page = ws.page();
			if(params.username == undefined)
				$page.append($('<h2>').text('Gezochte woorden'));
			else
				$page.append($('<h2>').text('Leerling: '+params.username));
			$table = $('<table>');
			//head
			var $row = $('<tr>');
				$row.append($('<td>').text('Woord'));
			if(params.username == undefined)
				$row.append($('<td>').append('Algemene definitie'));
			else
				$row.append($('<td>').text('Gegeven definitie'));
			$table.append($row);
			//body
			$.each(data, function(row, val){
				var $row = $('<tr>');
				$row.append($('<td>').text(val['word']));
				$row.append($('<td>').text(val['definition']));
				$row.click(function(){
					ws.send('getWordInfo',{
						word: val['word']
					});
				});
				$table.append($row);
			});
			$page.append($table);
		}
	},
	
	getPupils:{
		done: function(data){
			$page = ws.page();
			$page.append($('<h2>').text('Mijn leerlingen'))
			$table = $('<table>');
			//head
			var $row = $('<tr>');
				$row.append($('<td>').text('Naam'));
			$table.append($row);
			//body
			$.each(data, function(row, val){
				$row = $('<tr>');
				$row.append($('<td>').text(val['username']));
				$row.click(function(){
					ws.send('getWords',{
						userid: val['id'],
						username: val['username']
					});
				});
				$table.append($row);
			});
			$page.append($table);
		}
	},
	
	getWordInfo:{
		done: function(data, params){
			$page = ws.page();
			$page.append($('<h2>').text('Woord: '+params.word));
			$table = $('<table>');
			//head
			var $row = $('<tr>');
				$row.append($('<td>').text('Leerling'));
				$row.append($('<td>').text('Gegeven definitie'));
			$table.append($row);
			//body
			$.each(data.list, function(row, val){
				$row = $('<tr>');
				$row.append($('<td>').text(val['username']));
				$row.append($('<td>').text(val['definition']));
				$row.click(function(){
					ws.send('getWords', {
						word: params.word,
						userid: val['userid'],
						username: val['username']
					});
				});
				$table.append($row);
			});
			$page.append($table);
		}
	},
	
	getDrawings: {
		done: function(data){
			$page = ws.page();
			$page.append($('<h2>').text('Tekeningen'));
			$.each(data, function(row, val){
				var $img = $('<img>');
					$img.attr({
						'src': 'drawings/'+val['id']+'.png',
						'alt': 'tekening'
					});
				$div = $('<div>');
				$div.addClass('drawing');
				$div.append($img);
				$div.append($('<a>').attr('href', '#').text('Verwijder').click(function(){
					ws.send('deleteDrawing', {
						id: val['id']
					});
					ws.send('getDrawings');
				}));
				$page.append($div);
			});
		}
	}
}

